using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Dialogs/Simple List", 1)]
	public class DialogSimpleList : MaterialDialog
	{
		[SerializeField]
		private DialogTitleSection m_TitleSection = new DialogTitleSection();

		[SerializeField]
		private VerticalScrollLayoutElement m_ListScrollLayoutElement;

		private List<DialogSimpleOption> m_SelectionItems;

		private OptionDataList m_OptionDataList;

		[SerializeField]
		private GameObject m_OptionTemplate;

		private Action<int> m_OnItemClick;

		public DialogTitleSection titleSection
		{
			get
			{
				return m_TitleSection;
			}
			set
			{
				m_TitleSection = value;
			}
		}

		public VerticalScrollLayoutElement listScrollLayoutElement
		{
			get
			{
				return m_ListScrollLayoutElement;
			}
			set
			{
				m_ListScrollLayoutElement = value;
			}
		}

		public List<DialogSimpleOption> selectionItems => m_SelectionItems;

		public OptionDataList optionDataList
		{
			get
			{
				return m_OptionDataList;
			}
			set
			{
				m_OptionDataList = value;
			}
		}

		private void OnEnable()
		{
			GetComponentInChildren<OverscrollConfig>().Setup();
		}

		public void Initialize(OptionDataList optionDataList, Action<int> onItemClick, string titleText, ImageData icon)
		{
			m_TitleSection.SetTitle(titleText, icon);
			m_OptionDataList = optionDataList;
			m_SelectionItems = new List<DialogSimpleOption>();
			Image childByName = m_OptionTemplate.GetChildByName<Image>("Icon");
			VectorImage childByName2 = m_OptionTemplate.GetChildByName<VectorImage>("Icon");
			if (m_OptionDataList.options.Count > 0 && m_OptionDataList.options[0].imageData != null)
			{
				if (m_OptionDataList.options[0].imageData.imageDataType == ImageDataType.Sprite)
				{
					UnityEngine.Object.DestroyImmediate(childByName2.gameObject);
				}
				else
				{
					UnityEngine.Object.DestroyImmediate(childByName.gameObject);
				}
			}
			else
			{
				UnityEngine.Object.DestroyImmediate(childByName.gameObject);
			}
			for (int i = 0; i < m_OptionDataList.options.Count; i++)
			{
				m_SelectionItems.Add(CreateSelectionItem(i));
			}
			float num = DialogManager.rectTransform.rect.height;
			LayoutGroup component = m_TitleSection.text.transform.parent.GetComponent<LayoutGroup>();
			if (component.gameObject.activeSelf)
			{
				component.CalculateLayoutInputVertical();
				num -= component.preferredHeight;
			}
			m_ListScrollLayoutElement.maxHeight = num - 48f;
			UnityEngine.Object.Destroy(m_OptionTemplate);
			m_OnItemClick = onItemClick;
			Initialize();
		}

		private DialogSimpleOption CreateSelectionItem(int i)
		{
			DialogSimpleOption component = UnityEngine.Object.Instantiate(m_OptionTemplate).GetComponent<DialogSimpleOption>();
			component.rectTransform.SetParent(m_OptionTemplate.transform.parent);
			component.rectTransform.localScale = Vector3.one;
			component.rectTransform.localEulerAngles = Vector3.zero;
			RectTransform rectTransform = component.rectTransform;
			Vector3 localPosition = component.rectTransform.localPosition;
			float x = localPosition.x;
			Vector3 localPosition2 = component.rectTransform.localPosition;
			rectTransform.localPosition = new Vector3(x, localPosition2.y, 0f);
			OptionData optionData = m_OptionDataList.options[i];
			Text childByName = component.gameObject.GetChildByName<Text>("Text");
			Graphic childByName2 = component.gameObject.GetChildByName<Graphic>("Icon");
			childByName.text = optionData.text;
			if (optionData.imageData == null)
			{
				childByName.rectTransform.sizeDelta = new Vector2(-48f, 0f);
				childByName.rectTransform.anchoredPosition = new Vector2(0f, 0f);
				UnityEngine.Object.Destroy(childByName2.gameObject);
			}
			else
			{
				childByName2.SetImage(optionData.imageData);
			}
			component.index = i;
			DialogSimpleOption dialogSimpleOption = component;
			dialogSimpleOption.onClickAction = (Action<int>)Delegate.Combine(dialogSimpleOption.onClickAction, new Action<int>(OnItemClick));
			return component;
		}

		public void OnItemClick(int index)
		{
			m_OnItemClick.InvokeIfNotNull(index);
			m_OptionDataList.options[index].onOptionSelected.InvokeIfNotNull();
			Hide();
		}
	}
}
